library(tidyverse)
library(haven)
library(broom)
library(estimatr)


dat <- read_dta("/Users/f0029qh/Dropbox/0 Active Projects/Norway non-citizen voting/introprogram/replication/data/innvlevekaar2016_t.dta")

# Figure 4 (uncomment second line for Figure A8)
out <- dat  %>% 
  # filter(islam==1) %>%
  gather(dv_name, dv_value, std_emp,std_yrkinnt_1,std_norway_attachment,
                       std_ancestry_attachment,std_trust,std_democracy,
                       std_discrim,std_imfriends,std_gender_role_index,std_freedom_speech,std_stay_in_norway)%>%
  group_by(dv_name) %>%
  do(tidy(lm(dv_value ~ cohortXgroup +cohort + group  + 
               factor(Aldgrupp) + male +  father_edu + father_emp + emp_ancestry + islam + 
               fam_arrival + cntry_1 + cntry_2 + cntry_3  + cntry_4  + cntry_5  + cntry_6  + cntry_7 +
               + cntry_8  + cntry_9 + cntry_10 +  cntry_11 +  cntry_12, data = .,weights=vekter),conf.int=T))

out <- out %>% filter(term=="cohortXgroup") 
out <- as.data.frame(out)

out$dv_name[out$dv_name=="std_yrkinnt_1"] <- "Labor Income"
out$dv_name[out$dv_name=="std_emp"] <- "Employment"
out$dv_name[out$dv_name=="std_trust"] <- "Political Trust"
out$dv_name[out$dv_name=="std_discrim"] <- "Experienced Discrimination"
out$dv_name[out$dv_name=="std_norway_attachment"] <- "Attachment to Norway"
out$dv_name[out$dv_name=="std_ancestry_attachment"] <- "Attachment to Ancestry Country"
out$dv_name[out$dv_name=="std_democracy"] <- "Support for Democracy"
out$dv_name[out$dv_name=="std_imfriends"] <- "Ethnic Norwegian Friends"
out$dv_name[out$dv_name=="std_gender_role_index"] <- "Support Traditional Gender Roles"
out$dv_name[out$dv_name=="std_freedom_speech"] <- "Support Freedom of Speech"
out$dv_name[out$dv_name=="std_stay_in_norway"] <- "Desire to Live Long-Term in Norway"
out$dv_name[out$dv_name=="std_speak_norwegian_job"] <- "Speak Norwegian in Workplace"

out <- out %>%  arrange(estimate) %>% 
  mutate(dv_name2=factor(dv_name, levels=dv_name))

ggplot(out,aes(y=factor(dv_name2),x=estimate)) + geom_point() + 
  geom_errorbar(aes(xmin=conf.low,xmax=conf.high),width=0.1) + theme_bw() +
  ylab("") + xlab("Effect of Introductory Program Eligibility") + geom_vline(xintercept=0,linetype="dashed")

